package org.custom.spring.boot.security.config.security;

import org.custom.spring.boot.security.filters.*;
import org.custom.spring.boot.security.service.impl.SecurityFilterChainManagerImpl;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.access.ExceptionTranslationFilter;
import org.springframework.security.web.authentication.AnonymousAuthenticationFilter;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter;

import javax.annotation.Resource;

/**
 * Spring-Security相关配置
 * <p>
 * 开启接口权限校验
 *
 * @author LiKun
 * @date 2021/11/8 17:43
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class CustomSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
    @Resource
    private AnonymousFilter anonymousFilter;
    @Resource
    private AuthenticationFilter authenticationFilter;
    @Resource
    private HttpAccessDeniedHandler httpAccessDeniedHandler;
    @Resource
    private AuthenticationFrontFilter authenticationFrontFilter;
    @Resource
    private SecurityTokenDisposeFilter securityTokenDisposeFilter;
    @Resource
    private HttpAuthenticationEntryPoint httpAuthenticationEntryPoint;
    @Resource
    private RememberAuthenticationFilter rememberAuthenticationFilter;

    /**
     * 过滤对某些请求的拦截
     *
     * @param web 相关对象
     */
    @Override
    public void configure(WebSecurity web) {
        web.ignoring().antMatchers(SecurityFilterChainManagerImpl.getIgnorePaths());
    }

    /**
     * 配置 HttpSecurity 对象
     *
     * @param http HttpSecurity
     *
     * @throws Exception 相关异常
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.addFilterAt(anonymousFilter, AnonymousAuthenticationFilter.class)
                .addFilterAt(authenticationFilter, UsernamePasswordAuthenticationFilter.class)
                .addFilterAt(rememberAuthenticationFilter, RememberMeAuthenticationFilter.class)
                .addFilterAfter(securityTokenDisposeFilter, ExceptionTranslationFilter.class)
                .addFilterBefore(authenticationFrontFilter, AuthenticationFilter.class)
                .authorizeRequests()
                .anyRequest().permitAll()
                .and()
                .exceptionHandling()
                .accessDeniedHandler(httpAccessDeniedHandler)
                .authenticationEntryPoint(httpAuthenticationEntryPoint)
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .csrf().disable();
    }
}
